﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using yq.IBase;
using yq.Business;
using yq.CacheHelp;

namespace BLOB.Controllers
{

    public class MainController : ApiController
    {
        /// <summary>
        /// 获取服务器列表
        /// </summary>
        /// <returns></returns>
     [AcceptVerbs("post", "get")]
        public IList<yq.Model.Logic.SysFileServer> LoadServer()
        {
            Func<DbHelper, IList<yq.Model.Logic.SysFileServer>> fun = (db) =>
            {
                var list= new SysFileServerDal().Where(obj => obj.IsOpen == yq.Enums.BooleanType.Yes).ToList(db);
                return list;
            };

            return DbExec.DoRead(fun);
        }

        /// <summary>
        /// 获取文件夹列表
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs("post","get")]
        public Dictionary<string,yq.Model.Logic.SysFileServer> Get()
        {
            Func<DbHelper, Dictionary<string, yq.Model.Logic.SysFileServer>> fun = (db) =>
                {
                   var dic = new Dictionary<string, yq.Model.Logic.SysFileServer>();
                   var list= new SysFileServerDal().Where().ToList(db);
                   var flist = new SysFolderDal().Where(obj => obj.IsDel == yq.Enums.BooleanType.No).ToList(db);
                   foreach (var info in flist)
                   {
                       dic.Add(info.FolderId, list.FirstOrDefault(obj=>obj.Sid==info.Sid));
                   }
                   return dic;
                };

            return DbExec.DoRead(fun);
        }

        /// <summary>
        /// 添加存储区域
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs("post")]
        public yq.Model.Logic.SysFileServer Add(string folderId)
        {
            Func<DbHelper, CaCheRefresh, yq.Model.Logic.SysFileServer> fun = (db, cb) =>
            {
                var serverDal = new SysFileServerDal();
                var folderDal = new SysFolderDal();
                yq.Model.Logic.SysFileServer server=new yq.Model.Logic.SysFileServer();
                var folder=folderDal.Where(obj=>obj.FolderId==folderId).ToModel(db, yq.IBase.Basis.RowLock.True);
                if (folder.IsLoad == false)
                {
                    server = serverDal.Where(obj => obj.IsOpen == yq.Enums.BooleanType.Yes).OderBy(obj => obj.Num).ToModel(db);
                    folder = new yq.Model.Logic.SysFolder()
                    {
                        CTime = DateTime.Now,
                        FolderId = folderId,
                        Num = 0,
                        IsDel = yq.Enums.BooleanType.No,
                        IsHttp = yq.Enums.BooleanType.Yes,
                        Sid = server.Sid
                    };
                    folder.FolderId = folderDal.Add(folder, folderId, db);
                    server.Num++;
                    serverDal.Update(server, db);
                    db.Commit();
                }
                else
                {
                    db.Rollback();
                }

                return server;
            };

            return DbExec.DoTrans(fun);
        }

        /// <summary>
        /// 获取一个文件夹信息
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs("post")]
        public yq.Model.Logic.SysFolder Folder(string id)
        {
            Func<DbHelper, yq.Model.Logic.SysFolder> fun = (db) =>
            {
                return new SysFolderDal().Where(obj => obj.FolderId == id).ToModel(db);
            };

            return DbExec.DoRead(fun);
        }

        /// <summary>
        /// 关闭存储区域
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AcceptVerbs("post", "get")]
        public bool Close(string id)
        {
            Func<DbHelper, CaCheRefresh, bool> fun = (db, cb) =>
            {
                var dal = new SysFolderDal();
                var info = dal.Where(obj => obj.FolderId == id).ToModel(db);
                info.IsDel = yq.Enums.BooleanType.Yes;
                var b = dal.Update(info, db) == 1;
                db.Commit();
                return b;
            };

            return DbExec.DoTrans(fun);
        }
    }
}
